<template>
  <div class="room-list-container">
    <el-row :gutter="30" class="room-grid">
      <el-col
          v-for="room in rooms"
          :key="room.id"
          :xs="24" :sm="12" :md="12" :lg="8" :xl="6"
      >
        <!-- 房间卡片容器 -->
        <div class="room-card-wrapper">
          <el-card class="room-card" shadow="hover">
            <!-- 图片轮播区 -->
            <div class="image-section">
              <el-carousel
                  v-if="room.imgUrlList?.length"
                  :interval="5000"
                  height="240px"
                  indicator-position="outside"
                  arrow="never"
              >
                <el-carousel-item
                    v-for="(img, index) in room.imgUrlList"
                    :key="index"
                >
                  <el-image
                      :src="img"
                      fit="cover"
                      class="carousel-image"
                      :preview-src-list="room.imgUrlList"
                  >
                    <template #error>
                      <div class="image-error">
                        <el-icon><Picture /></el-icon>
                        <span>图片加载失败</span>
                      </div>
                    </template>
                  </el-image>
                </el-carousel-item>
              </el-carousel>
              <div v-else class="image-placeholder">
                <el-icon><Picture /></el-icon>
                <span>暂无图片</span>
              </div>
            </div>

            <!-- 信息内容区 -->
            <div class="content-section">
              <!-- 标题区 -->
              <div class="title-row">
                <h3 class="room-name">{{ room.name }}</h3>
                <div class="status-tag" :class="'active'">
                  {{'可预约'}}
                </div>
              </div>

              <!-- 价格评分区 -->
              <div class="price-rating">
                <div class="price">
                  <span class="unit">¥</span>
                  <span class="value">{{ room.price.toFixed(2) }}</span>
                  <span class="duration">/小时</span>
                </div>
                <div class="rating">
                  <el-rate
                      v-model="room.avgRating"
                      disabled
                      :colors="['#FFC300', '#FFA500', '#FF8C00']"
                  />
                  <span class="rating-text">{{ room.avgRating.toFixed(1) }}</span>
                </div>
              </div>

              <!-- 规格标签区 -->
              <div class="specs">
                <div class="spec-item">
                  <el-icon><User /></el-icon>
                  <span>{{ room.size }}</span>
                </div>
                <div class="tags">
                  <el-tag
                      v-for="tag in getRoomTags(room.tagIdList)"
                      :key="tag.id"
                      type="info"
                      effect="plain"
                      size="small"
                      round
                  >
                    {{ tag.label }}
                  </el-tag>
                </div>
              </div>

              <!-- 描述文本 -->
              <div class="description">
                {{ room.description || '暂无房间描述' }}
              </div>
            </div>

            <!-- 操作按钮 -->
            <div class="action-section">
              <el-button
                  type="primary"
                  size="large"
                  class="book-btn"
                  @click="handleBook(room)"
              >
                <template #icon>
                  <el-icon><Calendar /></el-icon>
                </template>
                立即预约
              </el-button>
            </div>
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import {User,Calendar } from '@element-plus/icons-vue'

const props = defineProps({
  rooms: {
    type: Array,
    default: () => []
  },
  tags: {
    type: Array,
    default: () => []
  }
})

const showBookingDialog = ref(false)
const selectedRoom = ref(null)

// 获取房间标签
const getRoomTags = (tagIds) => {
  return props.tags.filter(tag => tagIds?.includes(tag.id))
}

// 处理预约
const handleBook = (room) => {
  location.href = `/shop/book/${room.id}`
}
</script>

<style scoped>
.room-list-container {
  padding: 20px 40px;
}

.room-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.room-card-wrapper {
  height: 100%;

  .room-card {
    height: 100%;
    border-radius: 16px;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);

    &:hover {
      transform: translateY(-8px);
      box-shadow: 0 12px 24px rgba(0, 0, 0, 0.1);
    }
  }
}

.image-section {
  position: relative;
  border-radius: 16px 16px 0 0;
  overflow: hidden;

  :deep(.el-carousel) {
    .el-carousel__indicators {
      bottom: 10px;

      .el-carousel__indicator {
        padding: 4px;

        .el-carousel__button {
          width: 8px;
          height: 8px;
          border-radius: 50%;
          background: rgba(255, 255, 255, 0.6);
          transition: all 0.3s;
        }

        &.is-active .el-carousel__button {
          width: 20px;
          border-radius: 4px;
          background: var(--el-color-primary);
        }
      }
    }
  }

  .image-error,
  .image-placeholder {
    height: 240px;
    background: #f8f9fa;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #909399;

    .el-icon {
      font-size: 40px;
      margin-bottom: 8px;
      color: #c0c4cc;
    }

    span {
      font-size: 14px;
    }
  }
}

.content-section {
  padding: 20px;

  .title-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;

    .room-name {
      margin: 0;
      font-size: 18px;
      font-weight: 600;
      color: #303133;
      line-height: 1.4;
    }

    .status-tag {
      font-size: 12px;
      padding: 4px 10px;
      border-radius: 14px;

      &.active {
        background: #e8f4ff;
        color: var(--el-color-primary);
      }

      &.inactive {
        background: #fef0f0;
        color: var(--el-color-danger);
      }
    }
  }

  .price-rating {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;

    .price {
      .unit {
        font-size: 16px;
        color: var(--el-color-primary);
      }

      .value {
        font-size: 24px;
        font-weight: 700;
        color: var(--el-color-primary);
        margin: 0 2px;
      }

      .duration {
        font-size: 14px;
        color: #909399;
      }
    }

    .rating {
      display: flex;
      align-items: center;

      :deep(.el-rate) {
        --el-rate-icon-size: 18px;
      }

      .rating-text {
        margin-left: 8px;
        font-size: 14px;
        color: #f7ba2a;
        font-weight: 500;
      }
    }
  }

  .specs {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 16px;

    .spec-item {
      display: flex;
      align-items: center;
      padding: 6px 12px;
      background: #f8f9fa;
      border-radius: 20px;
      font-size: 13px;
      color: #606266;

      .el-icon {
        margin-right: 6px;
        font-size: 14px;
      }
    }

    .tags {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
    }
  }

  .description {
    font-size: 13px;
    line-height: 1.6;
    color: #909399;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    min-height: 40px;
  }
}

.action-section {
  padding: 0 20px 20px;

  .book-btn {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 500;
    border-radius: 12px;
    transition: all 0.3s;

    .el-icon {
      margin-right: 8px;
    }

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(var(--el-color-primary-rgb), 0.3);
    }
  }
}
</style>